package com.addplus.townmall.server.queue_consumer.consumer;

import com.addplus.townmall.server.api.model.town.param.OrderTimeOutParam;
import com.addplus.townmall.server.api.model.town.param.ShopProductStockManagerParam;
import com.addplus.townmall.server.api.modelenum.QueueEnum;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 类名: OrderTimeOutConsumer
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2018/9/27 下午5:50
 * @description 类描述: 订单超时队列消费者
 */
@Component
@RabbitListener(queues = "order_queue")
public class OrderTimeOutConsumer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitHandler
    public void process(OrderTimeOutParam orderTimeOutParam, Message message, Channel channel) throws IOException {
        ShopProductStockManagerParam shopProductStockManagerParam = new ShopProductStockManagerParam();
        shopProductStockManagerParam.setType(2);
        shopProductStockManagerParam.setOrderNum(orderTimeOutParam.getOrderNum());
        shopProductStockManagerParam.setOrderId(orderTimeOutParam.getOrderId());
        if (orderTimeOutParam.getType() == 1) {
            // 使用普通商品队列消费者处理(包含普通商品、礼品卡、会员卡)
            shopProductStockManagerParam.setSubType(orderTimeOutParam.getSubType());
            rabbitTemplate.convertAndSend(QueueEnum.STOCK_MANAGER.getExchange(), QueueEnum.STOCK_MANAGER.getRoutingKey(), shopProductStockManagerParam);
        } else {
            // 使用拼团商品队列消费者处理
            rabbitTemplate.convertAndSend(QueueEnum.FIGHT_STOCK_QUEUQ.getExchange(), QueueEnum.FIGHT_STOCK_QUEUQ.getRoutingKey(), shopProductStockManagerParam);
        }
    }
}
